import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.csv.CSVPrinter;
import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import pojo.PersonInfos;
import pojo.SeatInfo;

import javax.swing.text.DateFormatter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalTime;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

public class searchPeople {

    public static void main(String[] args) {
        List<PersonInfos> infoList = new LinkedList<>();

        boolean isPerson = true;

        for (int i = 100455696; i < 100455726; i+=2) {
            infoList = getList(Integer.toString(i),infoList);
        }


        System.out.println("请输入查找人的姓名或座位号");

        Scanner inpurScanner = new Scanner(System.in);

        String infos = inpurScanner.nextLine();
        String personSeat = "";
        String seatPerson = "";

        if(infos.contains("F")){
            isPerson = false;
        }

        if(isPerson){
            personSeat = getPersonSeat(infoList, infos);
            System.out.println("您查询的结果是不是 ："+personSeat);

        }
        else{
            seatPerson = getSeatPerson(infoList,infos);
            System.out.println("您查询的结果是不是 ："+seatPerson);

        }



    }

    private static String getSeatPerson(List<PersonInfos> infoList, String infos) {
        String seat = "抱歉，这座位这没人!!!!";
        for (PersonInfos personInfos : infoList) {
            if(personInfos.getTitle().equals(infos)){
                seat = personInfos.getName();
            }
        }
        return seat;
    }

    private static String getPersonSeat(List<PersonInfos> infoList, String name) {
        String seat = "抱歉，您所查找的人今天没有去学习!!!!";
        for (PersonInfos personInfos : infoList) {
            if(personInfos.getName().equals(name)){
                seat = personInfos.getTitle();
            }

        }

        return seat;


    }


    private static List getList(String roomId,List infoList) {

        Date date = new Date();

        CloseableHttpClient httpClient = HttpClientBuilder.create().build();


        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

        String format = df.format(date);




        HttpGet httpGet = new HttpGet("http://10.21.95.57/ClientWeb/pro/ajax/device.aspx?byType=devcls&classkind=8&display=fp&md=d&room_id="+roomId+"&purpose=&selectOpenAty=&cld_name=default&date="+format+"&fr_start=16%3A50&fr_end=17%3A50&act=get_rsv_sta&_=1600851046351");


        CloseableHttpResponse response = null;
        try {
            // 由客户端执行(发送)Get请求
            response = httpClient.execute(httpGet);
            // 从响应模型中获取响应实体
            HttpEntity responseEntity = response.getEntity();
//            System.out.println("响应状态为:" + response.getStatusLine());
            if (responseEntity != null) {
//                System.out.println("响应内容长度为:" + responseEntity.getContentLength());


                String json = EntityUtils.toString(responseEntity);

                com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(json);




                String data = jsonObject.getString("data");



                JSONArray dataArray = jsonObject.getJSONArray("data");
                for (int i = 0; i < dataArray.size(); i++) {
//                    String owner = dataArray.getJSONObject(i).getJSONObject("ts").getString("owner");
                    JSONObject dataObject = dataArray.getJSONObject(i);
                    String title = dataObject.getString("title");
                    JSONArray ts = dataObject.getJSONArray("ts");
                    if(ts.size()!=0){
                        String owner = ts.getJSONObject(0).getString("owner");

                        PersonInfos tmpInfos = new PersonInfos(title, owner);
                        infoList.add(tmpInfos);
                    }
                }






//                System.out.println("响应内容为:" + json);


            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                // 释放资源
                if (httpClient != null) {
                    httpClient.close();
                }
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return  infoList;

    }
}
